﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using Urb.Weather.Core;
using Urb.Weather.Core.Container;

namespace Urb.Weather.Container.Unity
{
    [Export(typeof(IContainer))]
    public class UnityContainerFacade : IContainer
    {
        private IUnityContainer container;

        public UnityContainerFacade()
        {
            this.container = new UnityContainer();
        }

        public IContainer RegisterType<TFrom, TTo>() where TTo : TFrom
        {
            this.container.RegisterType<TFrom, TTo>();
            return this;
        }

        public IContainer RegisterTypeContainerControlled<TFrom, TTo>() where TTo : TFrom
        {
            this.container.RegisterType<TFrom, TTo>(new ContainerControlledLifetimeManager());
            return this;
        }

        public IContainer RegisterInstance<TFrom>(TFrom instance)
        {
            this.container.RegisterInstance<TFrom>(instance);
            return this;
        }

        public IContainer RegisterInstanceContainerControlled<TFrom>(TFrom instance)
        {
            this.container.RegisterInstance<TFrom>(instance, new ContainerControlledLifetimeManager());
            return this;
        }
        
        public T Resolve<T>()
        {
            return this.container.Resolve<T>();
        }

        public T Resolve<T>(Type type)
        {
            return (T) this.container.Resolve(type);
        }
    }
}
